using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Vin.Tool.AspNetCore.Singleton;

namespace Vin.Tool.AspNetCore.Extension;

public static class AuthorizationFilterContextExtension
{
    public static void Unauthorized(this AuthorizationFilterContext context, string msg = "未授权")
    {
        context.Result =
            new OkObjectResult(Activator.CreateInstance(VinApp.ApiResultType, StatusCodes.Status401Unauthorized, msg));
    }
    
    public static void Forbidden(this AuthorizationFilterContext context, string msg = "无权限访问")
    {
        context.Result =
            new OkObjectResult(Activator.CreateInstance(VinApp.ApiResultType, StatusCodes.Status403Forbidden, msg));
    }
    
    public static void InternalServerError(this AuthorizationFilterContext context, string msg = "服务器错误")
    {
        context.Result =
            new OkObjectResult(Activator.CreateInstance(VinApp.ApiResultType, StatusCodes.Status500InternalServerError, msg));
    }
}